/*Confidential and Proprietary, Subject to the Non-Disclosure Agreement,
 Version 1.0, between the Development Gateway Foundation, Inc and the
 Recipient Copyright 2001-2004 Development Gateway Foundation, Inc.

 Local Projects Database
 */
package ro.gateway.aida.db;

import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.EditDBConnectorForm;

/**
 * @author Mihai Postelnicu e-mail: mihai@ro-gateway.org
 */
public class DatabaseConfiguratorAction extends MultiTilesAction {
	private DBConnectorProperties db;

	/*
	 * (non-Javadoc)
	 * 
	 * @see ro.gateway.aida.struts.MultiTilesAction#modePrepare(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Properties p = new Properties();
		p.load(this.getServlet().getServletContext().getResourceAsStream(
				"/WEB-INF/dbpass.properties"));
		String savedPwd = p.getProperty("dbpassword");

			db = new DBConnectorProperties(this.getServlet()
					.getServletContext());
			db.loadConnectors();
		
			String mode = request.getParameter("mode");
			if (mode != null && mode.equals("loginPerform"))
				return modeLoginPerform(mapping, form, request, response);
		
			HttpSession session=request.getSession();
			String sessionPwd=(String)session.getAttribute("dbpassword");
			if(sessionPwd!=null && sessionPwd.equals(savedPwd)) {
			    return modeSelect(mapping, form, request, response);
			}


		return modeLoginForm(mapping, form, request, response);
	}

	public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String mode = request.getParameter("mode");
		if (mode != null && mode.equals("new"))
			return modeNew(mapping, form, request, response);
		if (mode != null && mode.equals("edit"))
			return modeEdit(mapping, form, request, response);		
		if (request.getParameter("delete")!=null)
			return modeDelete(mapping, form, request, response);		
		if (request.getParameter("logout")!=null)
			return modeLogout(mapping, form, request, response);		
		
		if (request.getParameter("current")!=null)
			return modeSetCurrent(mapping, form, request, response);		

		if (request.getParameter("save") != null)
			return modeSave(mapping, form, request, response);
		return modeLoginForm(mapping, form, request, response);
	}

	private ActionForward modeSetCurrent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] seldb = request.getParameterValues("selconn");
		if(seldb!=null && seldb.length!=0) { 
		    db.setCurrent(seldb[0]);
		    db.saveConnectors();
		}
	    return modeList(mapping, form, request, response);
	}
	
	private ActionForward modeSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List connectors = db.getConnectors();
		EditDBConnectorForm dbf = (EditDBConnectorForm) form;
		DBConnector c = db.getConnector(dbf.getName());

		DBConnector new_c;
		if (dbf.getType().equals("oracle"))
			new_c = new OracleDBConnector(dbf.getDb());
		else
			new_c = new PostgresDBConnector(dbf.getDb());

		new_c.setName(dbf.getName());
		new_c.setHost(dbf.getHost());
		new_c.setPassword(dbf.getPassword());
		new_c.setPort(dbf.getPort());
		new_c.setUser(dbf.getUser());

		if (c == null)
			db.getConnectors().add(new_c);
		else {
			db.getConnectors().remove(c);
			db.getConnectors().add(new_c);
		}
		
		db.saveConnectors();
		return modeList(mapping, form, request, response);
	}

	private ActionForward modeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List connectors = db.getConnectors();
		if (connectors==null || connectors.size() != 0)
			request.setAttribute("connectors", connectors);
			request.setAttribute("current", db.getCurrent());
		return mapping.findForward("dblist");
	}
	
	private ActionForward modeDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String[] delbm = request.getParameterValues("selconn");
		
		for (int i = 0; delbm!=null && i < delbm.length; i++) {
			DBConnector d=db.getConnector(delbm[i]);
			db.getConnectors().remove(d);
		}
		db.saveConnectors();
		return modeList(mapping,form,request,response);
	}

	private ActionForward modeLogout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	    HttpSession session=request.getSession();
	    session.invalidate();
	    return mapping.findForward("index");	    
	}


	
	private ActionForward modeEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EditDBConnectorForm dbf = (EditDBConnectorForm) form;
		List connectors = db.getConnectors();
		String edited=request.getParameter("name");
		DBConnector conn=db.getConnector(edited);
		if(conn==null) return mapping.findForward("fail");
		dbf.setHost(conn.getHost());
		dbf.setName(edited);
		dbf.setPassword(conn.getPassword());
		dbf.setPort(conn.getPort());
		dbf.setUser(conn.getUser());
		
		if(conn instanceof PostgresDBConnector) {
			dbf.setType("postgres");
			dbf.setDb(((PostgresDBConnector)conn).getDbName());
		}
		if(conn instanceof OracleDBConnector) {
			dbf.setType("oracle");
			dbf.setDb(((OracleDBConnector)conn).getTablespace());
		}
		
		
		return mapping.findForward("dbedit");
	}

	
	private ActionForward modeNew(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EditDBConnectorForm dbf = (EditDBConnectorForm) form;
		dbf.reset(mapping,request);
		return mapping.findForward("dbedit");
	}

	private ActionForward modeLoginPerform(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String dbpassword = request.getParameter("dbpassword");
		HttpSession session=request.getSession();
		session.setAttribute("dbpassword", dbpassword);
		return modeList(mapping, form, request, response);
	}

	private ActionForward modeLoginForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("dblogin");
	}


}